package com.dongbo.recorder.screenrecord.base;

import android.app.AppOpsManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationManagerCompat;

import com.dongbo.recorder.screenrecord.utils.eventbus.EventBusUtils;
import com.tbruyelle.rxpermissions3.RxPermissions;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import butterknife.ButterKnife;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * @author GuoZi
 * @date :2020/5/3 13:26
 * @description:
 */
public abstract class BaseActivity extends AppCompatActivity {
    public RxPermissions getRxPermissions() {
        return rxPermissions;
    }

    private CompositeDisposable mDisposables = new CompositeDisposable();

    public RxPermissions rxPermissions = new RxPermissions(this);
    private boolean useEventBus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        ButterKnife.bind(this);
        initView();
        initData();
        initListener();
        if (useEventBus()) {
            EventBusUtils.register(this);
        }
    }

    //布局
    protected abstract int getLayout();

    //初始化view
    protected abstract void initView();

    //初始化data
    protected abstract void initData();

    //点击事件
    protected abstract void initListener();

    //是否启用eventbus
    public boolean useEventBus() {
        return useEventBus;
    }

    public boolean setUseEventBus(boolean tOf) {
        useEventBus = tOf;
        return useEventBus;
    }


    /**
     * 显示toast
     *
     * @param msg
     */
    public void showToast(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        }

//        if (!isNotificationEnabled(this)) {
//            ToastUtils.show(msg);
//        } else {
////            showSToast(text);
//            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
//        }
    }


    private boolean isNotificationEnabled(Context context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return true;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
                && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
            ApplicationInfo appInfo = context.getApplicationInfo();
            String pkg = context.getApplicationContext().getPackageName();
            int uid = appInfo.uid;
            Class appOpsClass = null; /* Context.APP_OPS_MANAGER */
            try {
                appOpsClass = Class.forName(AppOpsManager.class.getName());
                Method checkOpNoThrowMethod = appOpsClass.getMethod("checkOpNoThrow", Integer.TYPE, Integer.TYPE, String.class);
                Field opPostNotificationValue = appOpsClass.getDeclaredField("OP_POST_NOTIFICATION");
                int value = (int) opPostNotificationValue.get(Integer.class);
                return ((int) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            NotificationManagerCompat mNm = NotificationManagerCompat.from(context);
            return mNm.areNotificationsEnabled();
        }
        return true;
    }

    public void subscribe(Disposable disposable) {
        mDisposables.add(disposable);
    }

    public void unsubscribe() {
        if (mDisposables != null && !mDisposables.isDisposed()) {
            mDisposables.dispose();
            mDisposables.clear();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //注销eventbus
        if (useEventBus()) {
            EventBusUtils.unregister(this);
        }
    }

//    @Override
//    protected void onPause() {
//        super.onPause();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                //检测当前APP是否安全
//                if (!AntiHiJack.checkActivity(BaseActivity.this)) {//不安全
//                    runOnUiThread(() -> {
//                        ToastUtils.show("注意，当前APP已在后台运行!");
//                    });
//                }
//            }
//        }).start();
//    }

}
